package com.zh.ssmshirofreemarker.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.zh.ssmshirofreemarker.Utils.ResponseUtil;
import com.zh.ssmshirofreemarker.entity.Permission;
import com.zh.ssmshirofreemarker.entity.Role;
import com.zh.ssmshirofreemarker.service.IRolePermissionService;
import com.zh.ssmshirofreemarker.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZH
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @Autowired
    IRolePermissionService rolePermissionService;


    @RequestMapping(method = RequestMethod.GET)
    public String init(){
        return "/role/init";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public ResponseUtil doAdd(Role role){
        return ResponseUtil.check(roleService.addRole(role));
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseUtil updateRole(Role role){
        return ResponseUtil.check(roleService.updateRole(role));
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseUtil deleteRole(String ids){
        return ResponseUtil.check(roleService.deleteRoles(ids.split(",")));
    }

    @ResponseBody
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseUtil getRole(Map<String,Object> map,@PathVariable("id") String id){
        Long lid  = 0L;
        try{
            lid = Long.valueOf(id);
        } catch (Exception e){
            e.printStackTrace();
        }
        Role role = new Role();
        role.setRoleId(lid);
        return ResponseUtil.success(roleService.selectById(role));
    }

    @ResponseBody
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Page<Role> getAll(Page<Role> page, Role role){
        return roleService.getAll(page,role);
    }

    @RequestMapping("/add")
    public String addView(Map<String,Object> map){
        map.put("type","add");
        return "/role/add-edit";
    }

    @RequestMapping("/edit/{id}")
    public String editView(Map<String,Object> map,@PathVariable("id") String id){
        Long lid = 0L;
        try{
            lid = Long.valueOf(id);
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        Role role = roleService.selectById(lid);
        map.put("role",role);
        map.put("type","edit");
        return "/role/add-edit";
    }

    @RequestMapping("/bindPermission/{id}")
    public String bindPermission(@PathVariable("id")Long roleId,Map<String,Object> map){
        List<Map<String,Object>> permissions = null;
        try{
            permissions = rolePermissionService.getAllPermissionsAndSignIsCheck(roleId);
            List<Long> hasPermissionIds = rolePermissionService.getPermissionsByRoleId(roleId);
            map.put("permissions",permissions);
            map.put("hasPermissionIds",hasPermissionIds);
        } catch (Exception e){
            e.printStackTrace();
        }
        return "/role/bindPermission";
    }

}

